package models;

import java.text.DateFormat;
import java.util.Collection;
import java.util.Date;
import javax.xml.bind.annotation.XmlAttribute;
import play.data.binding.As;
import siena.*;
import siena.core.lifecycle.PreInsert;
import siena.core.lifecycle.PreUpdate;

public class BaseModel extends Model {
	
	@Id(Generator.AUTO_INCREMENT)
	public Long id;
	@DateTime
    @As(value="dd/MM/yyyy")
	public Date created_time;
	@DateTime
    @As(value="dd/MM/yyyy")
	public Date updated_time;
	
	public static <T extends BaseModel> Query<T> all(Class T) {
		return Model.all(T);
	}
	
	public static <T extends BaseModel> T findById(Class T, Long id) {
		return (T) all(T).filter("id", id).get();
	}
		
	@PreInsert
	protected void changeCRUD() {
		this.updated_time = new Date();
		this.created_time = new Date();
	}
	
	@PreUpdate
	protected void history() {
		this.updated_time = new Date();
	}
	
	public static String format(Date date) {
		return DateFormat.getDateTimeInstance().format(date);
	}
	
	public static long inMillis(Date date) {
		return date.getTime();
	}
	
}